{{/* 服务层-源 */}}
{{- $PkgNameLowerCase := .PkgName | FirstCharLowerCase -}}
{{- $TableNameUpper := .TableName | FirstCharUpper -}}
package {{$PkgNameLowerCase}}Service

import (
	"github.com/Sir-yuChen/funAnalyse/global"
	"github.com/Sir-yuChen/funAnalyse/model"
	"github.com/Sir-yuChen/funAnalyse/model/common/ApiReturn"
	"github.com/Sir-yuChen/funAnalyse/utils"
	"go.uber.org/zap"
)

type {{$TableNameUpper}}Service struct{}

func New{{$TableNameUpper}}Service() *{{$TableNameUpper}}Service {
	return &{{$TableNameUpper}}Service{}
}

//Get{{$TableNameUpper}} 多条件查询
func (u *{{$TableNameUpper}}Service) Get{{$TableNameUpper}}(req *model.{{$TableNameUpper}}Model) (a ApiReturn.ApiReturnCode) {
    var mo []model.{{$TableNameUpper}}Model
    sql :=""
    obj := model.New{{$TableNameUpper}}Model()
    tableName := obj.TableName()
    sqlCondition := utils.ReflectHandleStuct(req)
    if sqlCondition !="" {
        sql =`select * from `+ tableName +` where ` + sqlCondition
    } else {
     	sql = `select * from ` + tableName + ` order by id desc  limit 100 `
     }
    err := global.GVA_DB.Raw(sql).Scan(&mo).Error
    if err != nil {
    	global.GVA_LOG.Error("Get{{$TableNameUpper}}异常", zap.Any("request", req), zap.Any("sql", sql), zap.Error(err))
    	return ApiReturn.ErrSystem
    }
    s :=ApiReturn.OK
    s.Data = mo
    return s
}

//Insert{{$TableNameUpper}}
func (u *{{$TableNameUpper}}Service) Insert{{$TableNameUpper}}(req *model.{{$TableNameUpper}}Model) (a ApiReturn.ApiReturnCode) {
    err := global.GVA_DB.Model(model.{{$TableNameUpper}}Model{}).Create(&req).Error
    if err != nil {
    	global.GVA_LOG.Error("Insert{{$TableNameUpper}}异常", zap.Any("request", req), zap.Error(err))
    	return ApiReturn.ErrSystem
    }
    return ApiReturn.OK
}
//Update{{$TableNameUpper}} 更新
func (u *{{$TableNameUpper}}Service) Update{{$TableNameUpper}}(req *model.{{$TableNameUpper}}Model) (a ApiReturn.ApiReturnCode) {
    if req.Id == 0  {
        return ApiReturn.ErrParam
    }
    err := global.GVA_DB.Model(model.{{$TableNameUpper}}Model{}).Updates(req).Error
    if err != nil {
    	global.GVA_LOG.Error("Update{{$TableNameUpper}}异常", zap.Any("request", req),zap.Error(err))
    	return ApiReturn.ErrSystem
    }
    return ApiReturn.OK
}
//Del{{$TableNameUpper}} 根据ID删除
func (u *{{$TableNameUpper}}Service) Del{{$TableNameUpper}}(req *model.{{$TableNameUpper}}Model) (a ApiReturn.ApiReturnCode) {
    if req.Id == 0  {
        return ApiReturn.ErrParam
    }
    err := global.GVA_DB.Model(model.{{$TableNameUpper}}Model{}).Where("id = ?", req.Id).Delete(model.{{$TableNameUpper}}Model{}).Error
    if err != nil {
    	global.GVA_LOG.Error("Del{{$TableNameUpper}}异常", zap.Any("request", req),zap.Error(err))
    	return ApiReturn.ErrSystem
    }
    return ApiReturn.OK
}
